package com.op.studyingliteapp.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.op.polites.android.GestureImageView;
import com.op.studyingliteapp.R;
import com.op.studyingliteapp.sender.Runtime;
import com.op.studyingliteapp.sender.ui.view.device.DeviceScanDialog;
import com.op.util.FileUtil;
import com.op.util.ScreenCapUtil;
import com.op.util.ScreenSendUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Genji on 2015/7/30.
 */
public class ScreenshotActivity extends FragmentActivity {

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    @Override
    protected void onStart() {
        super.onStart();
      
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        getMenuInflater().inflate(R.menu.actionbar, menu);
        MenuItem screenshotBtn = menu.findItem(R.id.screenshot_item);
        MenuItem projectionBtn = menu.findItem(R.id.projection_item);
        MenuItem deviceScanBtn = menu.findItem(R.id.router_item);
        Runtime.refreshStatus(projectionBtn,screenshotBtn, deviceScanBtn);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.router_item:
                new DeviceScanDialog(this).show();
                break;
            case R.id.projection_item:
                if (!Runtime.isOffLine()) {
                    Runtime.play();
                    if (Runtime.getView() != null) {
                        ScreenSendUtils.sendWithDely(Runtime.getView());
                    }

                }


                break;
            case R.id.screenshot_item:
                Log.i("ScreenshotActivity", "screenshot_item");
                if (FileUtil.hasSdCard()) {
                    Bitmap bitmap = ScreenCapUtil.init(Runtime.getView());
                    new Thread(new SaveScreenShotRunnable(bitmap)).start();

                } else

                {
                    Toast.makeText(getApplicationContext(), "请插入SD卡", Toast.LENGTH_LONG).show();
                }

                break;
            case android.R.id.home:
                onBackPressed();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    class SaveScreenShotRunnable implements Runnable {

        Bitmap bitmap;

        public SaveScreenShotRunnable(Bitmap bitmap) {
            this.bitmap = bitmap;
        }

        @Override
        public void run() {
            Looper.prepare();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
            File file = FileUtil.saveFile(inputStream, "/Screenshot", formatter.format(new Date()) + ".png", ScreenshotActivity.this);
            if (file != null) {
                Toast.makeText(getApplicationContext(), "截图已经保存在" + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
            }
            Looper.loop();
        }


    }
}